<?php
/**
 * A simple description for this script
 *
 * PHP Version 5.2.0 or Upper version
 *
 * @package    Iris
 * @author     Hidehito NOZAWA aka Suin <http://suin.asia>
 * @copyright  2010 Hidehito NOZAWA
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2
 *
 */

class Iris
{
	/**
	 * Names cosist of [a-z0-9_]
	 * Those are usually used for file names.
	 */
	public static $_controller;
	public static $_action;

	/**
	 * Names cosist of [A-Za-z0-9]
	 * Those are usually used for class or method names.
	 */
	public static $Controller;
	public static $Action;

	public static $cms = null;

	public static $dirname = null;
	public static $url     = null;

	protected static $lang = array();

	/**
	 * Main frame
	 */
	public static function setup()
	{
		if ( defined('IRIS_LOADED') ) return;

		define('IRIS_PATH', dirname(__FILE__));
		define('IRIS_DIR', basename(IRIS_PATH));

		if ( !defined('DS') )
		{
			define('DS', DIRECTORY_SEPARATOR);
		}

		spl_autoload_register(array(__CLASS__, 'autoload'));

		self::$cms  = Iris_Kernel_CmsAdapter::getInstance();
		self::$lang = Iris_Language_Manager::getCatalogue();

		define('IRIS_LOADED', true);
	}

	public static function execute($dirname, $isAdmin = false)
	{
		$controller = self::get('controller', 'default');
		$action     = self::get('action', 'default');

		self::$Controller = self::putintoClassParts($controller);
		self::$Action     = self::putintoClassParts($action);

		self::$_controller = self::putintoPathParts(self::$Controller);
		self::$_action     = self::putintoPathParts(self::$Action);

		self::$Action[0]  = strtolower(self::$Action[0]);

		self::$dirname = $dirname;
		self::$url     = self::$cms->moduleUrl.'/'.$dirname;

		if ( $isAdmin )
		{
			define('IRIS_ADMIN_MODE', true);
			$class = 'Iris_Controller_Admin'.self::$Controller;
		}
		else
		{
			$class = 'Iris_Controller_'.self::$Controller;
		}

		if ( !class_exists($class) )
		{
			die("Invalid Access");
		}

		$instance = new $class();
		$instance->main();

		unset($instance);
	}

	public static function block($blockName, $options = array(), $editMode = false)
	{
		self::$dirname = IRIS_DIR;
		self::$url     = self::$cms->moduleUrl.'/'.IRIS_DIR;
		$class = 'Iris_Blocks_'.$blockName;
		$instance = new $class($blockName, $options, $editMode);
		$instance->main();
		unset($instance);
	}

	public static function autoload($class)
	{
		if ( class_exists($class, false) ) return;
		if ( !preg_match('/^Iris_/', $class) ) return;

		$parts = explode('_', $class);
		$parts = array_map(array(__CLASS__, 'putintoPathParts'), $parts);

		$module = array_shift($parts);

		$class = implode('/', $parts);
		$path  = sprintf('%s/%s.php', IRIS_PATH, $class);

		if ( !file_exists($path) ) return;

		require $path;
	}

	/**
	 * Usefull functions
	 */
	public static function get($name, $default = null)
	{
		$request = ( isset($_GET[$name]) ) ? $_GET[$name] : $default;
		if ( get_magic_quotes_gpc() and !is_array($request) ) $request = stripslashes($request);
		return $request;
	}

	public static function post($name, $default = null)
	{
		$request = ( isset($_POST[$name]) ) ? $_POST[$name] : $default;
		if ( get_magic_quotes_gpc() and !is_array($request) ) $request = stripslashes($request);
		return $request;
	}

	public static function request($name, $default = null)
	{
		return self::post($name, self::get($name, $default));
	}

	public static function putintoClassParts($str)
	{
		$str = preg_replace('/[^a-z0-9_]/', '', $str);
		$str = explode('_', $str);
		$str = array_map('trim', $str);
		$str = array_diff($str, array(''));
		$str = array_map('ucfirst', $str);
		$str = implode('', $str);
		return $str;
	}

	public static function putintoPathParts($str)
	{
		$str = preg_replace('/[^a-zA-Z0-9]/', '', $str);
		$str = preg_replace('/([A-Z])/', '_$1', $str);
		$str = strtolower($str);
		$str = substr($str, 1, strlen($str));
		return $str;
	}

	public static function redirect($msg, $controller = null, $action = null, $params = array())
	{
		if ( preg_match('/^http/', $controller) )
		{
			$url = $controller;
		}
		else
		{
			$url = self::url($controller, $action, $params);
		}

		$msg = self::t($msg);

		self::$cms->redirect($url, 3, $msg);
	}

	public static function location($controller = null, $action = null, $extra)
	{
		$url = self::url($controller, $action, $extra);
		header('Location: '.$url);
		die;
	}

	public static function url($controller = null, $action = null, $extra = array())
	{
		$params = array();

		$urlRoot = self::$cms->moduleUrl.'/'.self::$dirname.'/';

		if ( $controller )
		{
			$params[] = 'controller='. $controller;
		}

		if ( $action )
		{
			$params[] = 'action='.$action;
		}

		if ( is_array($extra) )
		{
			$params[] = http_build_query($extra);
		}

		if ( defined('IRIS_ADMIN_MODE') )
		{
			$admin = 'admin/';
		}
		else
		{
			$admin = '';
		}

		$param = implode('&', $params);
		$url   = $urlRoot . $admin .'index.php?'.$param;
		return $url;
	}

	public static function t($message)
	{
		if ( isset(self::$lang[$message]) )
		{
			$message = self::$lang[$message];
		}

		if ( func_num_args() == 1 ) return $message;

		$params = func_get_args();

		foreach ( $params as $i => $param )
		{
			$message = str_replace('{'.$i.'}', $param, $message);
		}

		return $message;
	}

	public static function error($msg)
	{
		self::$cms->error($msg);
	}

	public static function &database()
	{
		return self::$cms->database();
	}

	public static function escapeHtml($string)
	{
		return htmlspecialchars($string, ENT_QUOTES);
	}

	public static function setSession($name, $value)
	{
		$_SESSION[self::$dirname][$name] = $value;
	}

	public static function session($name)
	{
		return ( isset($_SESSION[self::$dirname][$name]) ) ? $_SESSION[self::$dirname][$name] : false;
	}
}

?>
